package com.lxk.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lxk.pojo.Device;
import com.lxk.pojo.Rules;
import com.lxk.pojo.Users;
import com.lxk.redis.RedisKey;
import com.lxk.redis.RedisUtil;
import com.lxk.service.DeviceService;
import com.lxk.service.PlayService;
import com.lxk.service.UsersService;
import com.lxk.vo.CurrentRedisVO;
import com.lxk.vo.result.Result;
import com.lxk.vo.QueueUserVO;
import com.lxk.vo.screen.PlayingUserVO;
import com.lxk.vo.screen.ScreenInfoVO;
import com.lxk.vo.screen.WaitingUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author JohnSoybean
 * @create 2020/7/24 15:10
 * @description 显示屏控制器
 */
@RestController
@RequestMapping("/screen")
public class ScreenController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private PlayService playService;

    /**
     * 展示全部设备
     * @return
     */
    @GetMapping("/device")
    public Result<List<Device>> getDeviceList() {
        List<Device> list = deviceService.list();
        return Result.success(list);
    }

    /**
     * 获取排队队列
     * @return
     */
    @GetMapping("/info")
    public Result<ScreenInfoVO> getQueueList(String deviceNumber) {
        long nowMillis = System.currentTimeMillis();
        ScreenInfoVO infoVO = new ScreenInfoVO();
        ObjectMapper om = new ObjectMapper();
        //规则数据
        Rules rule = playService.getRule();
        infoVO.setRules(rule);
        //当前玩家
        PlayingUserVO playingUserVO = null;
        String currentKey = redisUtil.joinKey(RedisKey.CURRENT.getPrefix(), deviceNumber);
        String currentJson = (String) redisUtil.get(currentKey);
        long gameTime = rule.getRuleExpire();
        int currentStatus = 0;
        long currentBegin = nowMillis;
        if (null != currentJson) {
            try {
                CurrentRedisVO current = om.readValue(currentJson, CurrentRedisVO.class);
                if (current != null) {
                    if (null != current.getStatus() && current.getStatus() == 1) {  //正在游戏中
                        currentStatus = 1;
                        currentBegin = current.getBegin();
                        gameTime = (nowMillis - currentBegin) / 1000 ;    //当前进行时间 秒
                        Users users = usersService.getById(current.getUserId());
                        playingUserVO = new PlayingUserVO();
                        playingUserVO.setStatus(1);
                        playingUserVO.setUserId(current.getUserId());
                        playingUserVO.setUsername(users.getUsername());
                        playingUserVO.setHeadimg(users.getHeadimg());
                        playingUserVO.setGameTime(gameTime);
                    }
                    Long begin = current.getBegin();
                    if (begin != null) {
                        currentBegin = begin;
                    }
                }
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        infoVO.setPlayUser(playingUserVO);
        //队列数据
        Integer ruleExpire = rule.getRuleExpire();
        String queueKey = redisUtil.joinKey(RedisKey.QUEUE.getPrefix(), deviceNumber);
        List<Object> queue = redisUtil.lGet(queueKey, 0, -1);
        List<QueueUserVO> queueUserList = new ArrayList<>();
        if (null != queue && queue.size() > 0) {
            for (int i = 0; i < queue.size(); i++) {
                QueueUserVO vo = null;
                try {
                    vo = om.readValue((String) queue.get(i), QueueUserVO.class);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
                if (vo != null) {
                    long wait = ruleExpire * (i + 1) - gameTime;
                    vo.setWaitTime(wait);
                    queueUserList.add(vo);
                }
            }
        }
        // 等待扫码用户
        WaitingUserVO waitingUserVO = null;
        Integer ruleWait = rule.getRuleWait();
        if(queueUserList.size() > 0){
            QueueUserVO queueUserVO = queueUserList.get(0);
            waitingUserVO = new WaitingUserVO();
            waitingUserVO.setUserId(queueUserVO.getUserId());
            waitingUserVO.setHeadimg(queueUserVO.getHeadimg());
            waitingUserVO.setUsername(queueUserVO.getUsername());
            if (currentStatus == 1) {   //正在游戏中
                waitingUserVO.setStatus(0); //倒计时未开始
                waitingUserVO.setCountDown(null);
            } else {    //游戏已结束
                waitingUserVO.setStatus(1); //倒计时开始
                long waitTime = (nowMillis - currentBegin) / 1000;
                long countDown = ruleWait - waitTime;
                waitingUserVO.setCountDown(countDown);
            }
        }
        infoVO.setWaitUser(waitingUserVO);
        //去除首位等待用户
        if (queueUserList.size() > 0) {
            queueUserList.remove(0);
        }
        infoVO.setQueueUsers(queueUserList);
        return Result.success(infoVO);
    }


}
